package concurrency;

import java.io.IOException;

/**
 * Created by sunzhengjie on 2017/1/18.
 */
public class TestSingleton {

    private static Object singleton;

    public static Object getInstance() {
        singleton = new Object();
        return singleton;
    }

    public static Object threadSafeGetInstance(){
        if(singleton == null){
            synchronized (TestSingleton.class){
                if(singleton == null){
                    singleton = new Object();
                    return singleton;
                }
            }
        }
        return singleton;
    }

    public static void main(String[] arg0) throws IOException, InterruptedException {
        int i = 1000;
        while(i-- != 0){
            new Thread(){
                @Override
                public void run() {
                    if(getInstance() != (singleton)){
                        System.out.println("单例对象引用不同");
                    }
                }
            }.start();
        }
        Thread.sleep(1000);
    }
}
